import 'dart:convert';

import 'package:flutter/services.dart';

import 'empUtils/EMPCustomCell.dart';

import 'package:flutter/material.dart';
import 'empUtils/EMPJsonUtil.dart';

class EMPTestCustomCellTwoVC extends StatefulWidget {
  final String title;

  EMPTestCustomCellTwoVC({required this.title});

  @override
  _EMPTestCustomCellVCState createState() => _EMPTestCustomCellVCState();
}

class _EMPTestCustomCellVCState extends State<EMPTestCustomCellTwoVC> {
  List<Module> modules = [];
  Map<String, dynamic> model = {};

  @override
  void initState() {
    super.initState();
    fetchData(); // 在初始化时获取数据
  }

  // 模拟网络请求
  Future<void> fetchData() async {
    String jsonString = await rootBundle.loadString('lib/assets/testmodel.json');

    Map<String, dynamic> data = jsonDecode(jsonString);

    // 将解析后的数据转换为 Module 对象列表
    List<Module> newModules =
        List.from(data['modules']).map((json) => Module.fromJson(json)).toList();

    setState(() {
      modules = newModules; // 更新模块列表
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: ListView.custom(
          childrenDelegate: SliverChildBuilderDelegate(
            childCount: modules.length,
            (BuildContext context, int index) {
              Module module = modules[index];
              print('richtitle ${module.params.richtitle}');

              return EMPCustomCell(
                //EMPeDefaultCell，EMPAvatarTitleCell，EMPAvatarTitleIconCell,EMPTitleSwitchCell,EMPTitleBtnCell
                CustomCellType.EMPTitleSwitchCell,
                '${module.name} $index',
                () {
                  print('Cell $index 被点击了');
                },
                arrowIcon: 'other/ic_arrow_right',
                avatarIcon: 'other/icon_avatar',
                subtitle:
                    'SubtitleSubtitleSubtitletleSubtitletleSubtitleCelltleSubtitleCelltleSubtitle $index',
                subtitleTwo:
                    'SubtitleSubtitleSubtitletleSubtitletleSubtitleCelltleSubtitleCelltleSubtitle $index',
                rightTitle:
                    'SubtitleSubtitleSubtit哈letleSubtitletleSubtitleCelltleSubtitleCelltleSubtitle $index',
                switchValue: module.switchValue,
                onValueChanged: (value) {
                  print('$value');

                  Module module1 = modules[index + 1];
                  module1.name = '这是刷新后的数据';
                  Future.delayed(Duration(seconds: 2), () {
                    setState(() {});
                  });
                },
                clickBtn: () {
                  print('点击按钮');
                },
                richTitle: module.params.richtitle,
                richColor: '1B6EFD',
              );
            },
          ),
        ));
  }
}

class Module {
  bool switchValue;
  int age;
  String name;
  String subtitle;
  String subtitleTwo;
  Params params;

  Module({
    required this.switchValue,
    required this.age,
    required this.name,
    required this.subtitle,
    required this.subtitleTwo,
    required this.params,
  });

  factory Module.fromJson(Map<String, dynamic> json) {
    return Module(
      switchValue: json['switch'],
      age: json['age'],
      name: json['name'],
      subtitle: json['subtitle'],
      subtitleTwo: json['subtitleTwo'],
      params: Params.fromJson(json['params']),
    );
  }
}

class Params {
  String richtitle;

  Params({
    required this.richtitle,
  });

  factory Params.fromJson(Map<String, dynamic> json) {
    return Params(
      richtitle: json['richtitle'] ?? '',
    );
  }
}
